using System;
using System.Collections;
using System.Data.Linq.Mapping;
using System.Runtime.Serialization;
using Portal.Data.Helper;
using Portal.Core.Binding.FormBuilder;
using Portal.Core.Helper;

namespace Portal.Data.Entity
{
    [Table(Name = "dbo.Module"), Serializable, DataContractAttribute]
    public class Module : PortalObject
    {
        #region Properties
        [JoinKey(Type = typeof(PortalObject))]
        public new long ID
        {
            get
            {
                return Module_ID;
            }
            set { Module_ID = value; }
        }

        [SkipBinding]
        [Column(IsPrimaryKey = true)]
        public long Module_ID { get; set; }

        [Column]
        public string Name { get; set; }

        [Column, Unique]
        public string Page { get; set; }

        [Column, SkipBinding]
        public string FullName { get; set; }

        [Column]
        public bool Hidden { get; set; }

        #endregion

        #region Methods
        public new static Module FindByPrimaryKey(object objectid)
        {
            return FindByPrimaryKey<Module>(objectid);
        }
        public static IList ListAll()
        {
            //getcache  -----------------------
            var parameter = new object[] { typeof(Module).Name, "ListAll" };
            var obj = CacheHelper.Instance.Get(parameter);
            if (obj == null)
            {
                var result = LINQHelper.GetList<Module>("select * from Module order by Name");
                CacheHelper.Instance.Set(parameter, result);
                return result;
            }
            return (IList)obj;
        }

        public static IList ListAvailable()
        {
            //getcache  -----------------------
            var parameter = new object[] { typeof(Module).Name, "ListAvailable" };
            var obj = CacheHelper.Instance.Get(parameter);
            if (obj == null)
            {
                var result = LINQHelper.GetList<Module>("select * from Module where Hidden={0} order by Name", false);
                CacheHelper.Instance.Set(parameter, result);
                return result;
            }
            return (IList)obj;
        }
        public static Module FindByPageName(string name)
        {
            //getcache  -----------------------
            var parameter = new object[] { typeof(Module).Name, "FindByPageName", name };
            var obj = CacheHelper.Instance.Get(parameter);
            if (obj != null)
                return (Module)obj;
            //end get cache -------------------

            var result = LINQHelper.GetFirst<Module>("select * from Module where Page={0}", name);
            CacheHelper.Instance.Set(parameter, result);
            return result;
        }
        #endregion Methods

    }
}
